/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.geometry;

import java.util.ArrayList;
import java.util.List;

import org.openaecfm.model.geometry.BSplineSurfaceWithKnots;
import org.openaecfm.model.geometry.RationalBSplineSurfaceWithKnots;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.ListParameter;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.RealParameter;
import org.openaecfm.spf.model.SimpleEntityInstance;

public final class RationalBSplineSurfaceWithKnotsIO extends
		AbstractEntityInstanceInputImpl<RationalBSplineSurfaceWithKnots>
		implements IEntityInstanceOutput<RationalBSplineSurfaceWithKnots> {

	private IEntityInstanceUtilOutput<BSplineSurfaceWithKnots> utilOutput;
	private IEntityInstanceUtilInput<BSplineSurfaceWithKnots> utilInput;

	public void setUtilOutput(
			IEntityInstanceUtilOutput<BSplineSurfaceWithKnots> utilOutput) {
		this.utilOutput = utilOutput;
	}

	public void setUtilInput(
			IEntityInstanceUtilInput<BSplineSurfaceWithKnots> utilInput) {
		this.utilInput = utilInput;
	}

	@Override
	public String exportSPF(RationalBSplineSurfaceWithKnots pojo,
			IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance(
				"IFCRATIONALBSPLINESURFACEWITHKNOTS",
				appDataManager.getName(pojo));

		utilOutput.exportSPF(pojo, ei, appDataManager);

		// Set WeightsData
		ListParameter data = new ListParameter();
		for (List<Double> ld : pojo.getWeightsData()) {
			ListParameter l = new ListParameter();
			for (Double d : ld) {
				l.add(new RealParameter(d));
			}
			data.add(l);
		}
		ei.addParameter(data);

		return ei.toString();
	}

	@Override
	public void resolveReferenceParameter(EntityInstance ei,
			RationalBSplineSurfaceWithKnots pojo, IAppDataManager appDataManager) {
		utilInput.resolveReferenceParameter(ei, pojo, appDataManager);
	}

	@Override
	protected RationalBSplineSurfaceWithKnots doCreateObject() {
		return new RationalBSplineSurfaceWithKnots();
	}

	@Override
	protected void resolveNotReferenceParameter(EntityInstance ei,
			RationalBSplineSurfaceWithKnots pojo) {
		utilInput.resolveNotReferenceParameter(ei, pojo);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set WeightsData
		ListParameter data = (ListParameter) sei.getParameter(12);
		for (Parameter p : data) {
			ListParameter lp = (ListParameter) p;
			List<Double> ld = new ArrayList<Double>();
			for (Parameter pa : lp) {
				RealParameter r = (RealParameter) pa;
				ld.add(r.toDouble());
			}
			pojo.getWeightsData().add(ld);
		}
	}
}
